package com.leyou.item.service.impl;

import com.leyou.common.constant.MessageConstant;
import com.leyou.common.enums.ItemExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.utils.JsonUtils;
import com.leyou.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.mapper.SpuMapper;
import com.leyou.item.service.SpecParamService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @author: Ljxpa
 * @date: 2020/03/07 16:31
 * @description:
 */
@Service
@Slf4j
public class SpecParamServiceImpl implements SpecParamService {

	@Autowired
	private SpecParamMapper specParamMapper;

	@Autowired
	private SpuMapper spuMapper;

	/**
	 * 根据参数组id查询该组内所有规格参数
	 *
	 * @param gid 参数规格组的id
	 * @param cid 品类的id
	 * @return 规格参数的list集合
	 */
	@Override
	public List<SpecParamDTO> queryParamsByGroupId(Long gid, Long cid) {
		SpecParam specParam = new SpecParam();
		specParam.setGroupId(gid);
		specParam.setCid(cid);
		List<SpecParam> specParamList = null;
		try {
			specParamList = specParamMapper.select(specParam);
		} catch (Exception e) {
			log.error(MessageConstant.QUERY_SPECPARAMS_DB_ERROR);
			throw new LyException(ItemExceptionEnum.DATA_QUERY_ERROR);
		}
		List<SpecParamDTO> specParamDTOList = BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
		if (CollectionUtils.isEmpty(specParamDTOList)) {
			throw new LyException(ItemExceptionEnum.SPEC_PARAM_NOT_FOUND);
		}
		return specParamDTOList;
	}

	/**
	 * 根据规格参数的id删除规格参数
	 * @param paramId 规格参数的id
	 */
	@Override
	@Transactional
	public void delSpecParamById(Long paramId) {
		try {
			SpecParam specParam = specParamMapper.selectByPrimaryKey(paramId);
			//根据品类id查询该品类下是否有spu
			SpuDetail spuDetail = spuMapper.querySpuDetailByCid(specParam.getCid());
			if (null != spuDetail) {
				//有spu，遍历spu是否引用了该参数
				Map<Long, String> map = JsonUtils.toMap(spuDetail.getGenericSpec() + spuDetail.getSpecialSpec(), Long.class, String.class);
				if (map.containsKey(paramId)) {
					//如果spu中使用了规格参数，则不能删除
					throw new LyException(ItemExceptionEnum.DATA_NOT_ALLOW_DELETE);
				}
			}

			//未被sku引用，可以删除
			int delete = specParamMapper.delete(specParam);
			if (0 == delete) {
				throw new LyException(ItemExceptionEnum.DATA_DELETE_ERROR);
			}
		} catch (Exception e) {
			log.error("数据库异常");
			throw new LyException(ItemExceptionEnum.DB_SERVICE_ERROR);
		}
	}

	@Override
	@Transactional
	public void editSpecParam(SpecParamDTO specParamDTO) {
		SpecParam specParam = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
		try {
			specParam.setUpdateTime(DateTime.now().toDate());
			int count = specParamMapper.updateByPrimaryKey(specParam);
			if (count == 0) {
				throw new LyException(ItemExceptionEnum.DATA_UPDATE_ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(MessageConstant.UPDATE_SPEC_PARAM_ERROR);
			throw new LyException(ItemExceptionEnum.DATA_UPDATE_ERROR);
		}

	}

	/**
	 * 修改规格参数，直接修改，未设置业务逻辑
	 *
	 * @param specParamDTO specParamDto对象
	 */
	@Override
	@Transactional
	public void addSpecParam(SpecParamDTO specParamDTO) {
		SpecParam specParam = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
		try {
			specParam.setCreateTime(DateTime.now().toDate());
			int count = specParamMapper.insert(specParam);
			if (count == 0) {
				throw new LyException(ItemExceptionEnum.DATA_SAVE_ERROR);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(MessageConstant.SAVE_SPEC_PARAM_ERROR);
			throw new LyException(ItemExceptionEnum.DATA_SAVE_ERROR);
		}
	}

	/**
	 * 根据第三级品类id查询所有可用来搜索的规格参数
	 * @param cid 第三级品类id
	 * @param searching 是否可搜索的布尔值
	 * @return 所有可用来搜索的规格参数的集合List<SpecParamDTO>
	 */
	@Override
	public List<SpecParamDTO> querySearchingParamsByCid(Long cid, Boolean searching) {
		SpecParam record = new SpecParam();
		record.setCid(cid);
		record.setSearching(searching);
		List<SpecParam> specParamList = specParamMapper.select(record);
		return BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
	}
}
